var http = require("../../utils/http.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    identityCardNumber: "",
    identityCardPicBack: "",
    identityCardPicFront: "",
    identityCardPicHold: "",
    _identityCardPicBack: "",
    _identityCardPicFront: "",
    _identityCardPicHold: "",
    realName: "",
    sharerCardNo: "",
    shopId: 1,
    userMobile: "",

    needIdCardNo:false,
    neddIdCardPic:false,
    needRealName:false,

    distributionCardNo:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading();
    var params = {
      url: "/p/distribution/register/condition",
      method: "GET",
      data: {
        shopId: 1,
      },
      callBack: (res) => {
        this.setData({
          needIdCardNo: res.identityCardNumber,
          neddIdCardPic: res.identityCardPic,
          needRealName: res.realName
        })
        wx.hideLoading();
      }
    };
    http.request(params);

    if (options.scene) {
      const scene = decodeURIComponent(options.scene);
      this.setData({
        distributionCardNo: scene
      });
    }
  },

  onMobileInput: function (e) {
    this.setData({
      userMobile: e.detail.value
    });
  },

  onRealNameInput: function (e) {
    this.setData({
      realName: e.detail.value
    });
  },

  onIdCardNoInput: function (e) {
    this.setData({
      identityCardNumber: e.detail.value
    });
  },

  uploadIdCardFront:function(){
    var ths = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
         wx.showLoading({
            mask: true
          })
          var params = {
            url: "/p/file/upload",
            filePath: tempFilePaths[0],
            name: 'file',
            callBack: function (res2) {
              wx.hideLoading();
              ths.setData({
                _identityCardPicFront: JSON.parse(res2).resourcesUrl + JSON.parse(res2).filePath,
                identityCardPicFront: JSON.parse(res2).filePath
              })
            }
          };
          http.upload(params);
      }
    })
  },

  uploadIdCardBack: function () {
    var ths = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          mask: true
        })
        var params = {
          url: "/p/file/upload",
          filePath: tempFilePaths[0],
          name: 'file',
          callBack: function (res2) {
            wx.hideLoading();
            ths.setData({
              _identityCardPicBack: JSON.parse(res2).resourcesUrl + JSON.parse(res2).filePath,
              identityCardPicBack: JSON.parse(res2).filePath
            })
          }
        };
        http.upload(params);
      }
    })
  },

  uploadIdCardHold: function () {
    var ths = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          mask: true
        })
        var params = {
          url: "/p/file/upload",
          filePath: tempFilePaths[0],
          name: 'file',
          callBack: function (res2) {
            wx.hideLoading();
            ths.setData({
              _identityCardPicHold: JSON.parse(res2).resourcesUrl + JSON.parse(res2).filePath,
              identityCardPicHold: JSON.parse(res2).filePath
            })
          }
        };
        http.upload(params);
      }
    })
  },

  /**
   * 提交申请
   */
  submitDisterApply:function(){
    var identityCardNumber = this.data.identityCardNumber;
    var identityCardPicBack = this.data.identityCardPicBack;
    var identityCardPicFront = this.data.identityCardPicFront;
    var identityCardPicHold = this.data.identityCardPicHold;
    var distributionCardNo = this.data.distributionCardNo;
    var realName = this.data.realName;
    var userMobile = this.data.userMobile;

    var needIdCardNo = this.data.needIdCardNo;
    var neddIdCardPic = this.data.neddIdCardPic;
    var needRealName = this.data.needRealName;

    if(userMobile.length!=11){
      wx.showToast({
        title: '请输入正确的手机号码',
        icon:"none"
      })
    } else if (needRealName && realName.length<2) {
      wx.showToast({
        title: '请输入真实姓名',
        icon: "none"
      })
    } else if (needIdCardNo && identityCardNumber.length < 15) {
      wx.showToast({
        title: '请输入正确的身份证号码',
        icon: "none"
      })
    } else if (neddIdCardPic && identityCardPicFront=="") {
      wx.showToast({
        title: '请上传身份证人像面',
        icon: "none"
      })
    } else if (neddIdCardPic && identityCardPicBack == "") {
      wx.showToast({
        title: '请上传身份证国徽面',
        icon: "none"
      })
    } else if (neddIdCardPic && identityCardPicHold == "") {
      wx.showToast({
        title: '请上传手持身份证照片',
        icon: "none"
      })
    }else{
      wx.showLoading({
        mask: true
      })
      var params = {
        url: "/p/distribution/register/addDistributionUser",
        method: "post",
        data: {
          identityCardNumber: identityCardNumber,
          identityCardPicBack: identityCardPicBack,
          identityCardPicFront: identityCardPicFront,
          identityCardPicHold: identityCardPicHold,
          realName: realName,
          sharerCardNo: distributionCardNo,
          shopId: 1,
          userMobile: userMobile,
        },
        callBack: (res) => {
          wx.hideLoading();
          wx.showModal({
            content: '申请已提交，请等待审核',
            showCancel:false,
            complete:function(){
              wx.switchTab({
                url: '/pages/user/user',
              })
            }
          })
        }
      };
      http.request(params);
    }

  },





})